package com.powerfl.frm.taf{
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.utils.*;
	
	final public class TAFbitmapBlitChild{
		
		static private var matrix:Matrix = new Matrix;
		static private var point:Point = new Point;
		static private var colorTrans:ColorTransform = new ColorTransform;
		
		public var x:Number = 0;
		public var y:Number = 0;
		public var tx:Number = 0;
		public var ty:Number = 0;
		public var scaleX:Number = 1;
		public var scaleY:Number = 1;
		public var alpha:Number = 1;
		public var rotation:Number = 0;
		public var visible:Boolean = true;
		public var ownCanvas:Boolean = true;
		
		public var name:String = '';
		public var depth:uint;
		public var useHandCursor:Boolean;
		
		private var _width:Number = 1;
		private var _height:Number = 1;
		
		public function get width():Number{
			return _width * scaleX;
		}
		public function set width( $val:Number ):void{
			scaleX = $val/_width;
		}
		public function get height():Number{
			return _height * scaleY;
		}
		public function set height( $val:Number ):void{
			scaleY = $val/_height;
		}
		public function set material( $material:* ):void{
			_bitmapData = $material.bitmap;
			_width = _bitmapData.width;
			_height = _bitmapData.height;
			if( scaleX === 1 ){}else{
				scaleX /= _width;
			}
			if( scaleY === 1 ){}else{
				scaleY /= _height;
			}
			TAF.SELF.collectMaterial( $material );
		}
		
		private var _px:Number = 0;
		private var _py:Number = 0;
		private var _ptx:Number = 0;
		private var _pty:Number = 0;
		private var _psx:Number = 0;
		private var _psy:Number = 0;
		private var _pa:Number = 0;
		private var _pr:Number = 0;
		private var _pv:Boolean = true;
		private var _pw:Number = 0;
		private var _ph:Number = 0;
		
		private var _parent:TAFbitmapBlit;
		private var _bitmapData:BitmapData;
		private var _color:BitmapData;
		
		public function TAFbitmapBlitChild(){}
		internal function init( $parent:TAFbitmapBlit, $depth:uint ):void{
			_parent = $parent;
			depth = $depth;
			point.x = point.y = 0;
			_color = new BitmapData( _bitmapData.width, _bitmapData.height, true, 0 );
			_color.copyPixels( _bitmapData, _bitmapData.rect, point, null, null, true );
			_color.threshold( _color, _color.rect, point, '>', 0x00ffffff, depth, 0xffffffff );
			_px = x+1;
			_py = y;
			_ptx = tx;
			_pty = ty;
			_psx = scaleX = 1;
			_psy = scaleY = 1;
			_pa = alpha;
			_pr = rotation;
			_pv = visible;
			_pw = _width;
			_ph = _height; 
		}
		internal function collect():TAFbitmapBlitChild{
			x = y = tx = ty = rotation = 0;
			scaleX = scaleY = _width = _height = 1;
			alpha = 0;
			visible = true;
			name = '';
			_bitmapData.dispose();
			_color.dispose();
			_parent = null;
			delete _parent._down[this];
			delete _parent._up[this];
			delete _parent._over[this];
			delete _parent._out[this];
			return this;
		}
		internal function isUpdate():int{
			var i:int;
			if( _px === x )if( _py === y )if( _ptx === tx )if( _pty === ty )if( _psx === scaleX )if( _psy === scaleY )
				if( _pa === alpha )if( _pr === rotation )if( _pv === visible )if( _pw === _width )if( _ph === _height ){ 
					return 0;
				}
			return -1;
		}
		internal function render():void{
			_px = x;
			_py = y;
			_ptx = tx;
			_pty = ty;
			_psx = scaleX;
			_psy = scaleY;
			_pa = alpha;
			_pr = rotation;
			_pv = visible;
			_pw = _width;
			_ph = _height;
			if( visible ){
				if( rotation === 0 && scaleX === 1  && scaleY === 1 && alpha === 1  ){
					point.x = x;
					point.y = y;
					_parent.bitmapData.copyPixels( _bitmapData, _bitmapData.rect, point, _bitmapData, null, true );
					_parent._depthData.copyPixels( _color, _color.rect, point, _bitmapData, null, true );
				}else{
					matrix.identity();
					if( rotation ){
						matrix.translate( -tx, -ty );
						matrix.rotate( TAF.SELF.mathToRadian( rotation ) );
					}
					if( scaleX === 1  && scaleY === 1 ){}else{
						matrix.scale( scaleX, scaleY );
					}
					if( x || y ){
						matrix.translate( x, y );
					}
					colorTrans.alphaMultiplier = alpha;
					_parent.bitmapData.draw( _bitmapData, matrix, colorTrans, null, _parent._rect, true );
					_parent._depthData.draw( _color, matrix, null, null, _parent._rect, false );
				}
			}
		}
		internal function addOnMouseDown( $listener:Function ):void{
			addEvent( '_down', $listener );
		}
		internal function addOnMouseUp( $listener:Function ):void{
			addEvent( '_up', $listener );
		}
		internal function addOnMouseOver( $listener:Function ):void{
			addEvent( '_over', $listener );
		}
		internal function addOnMouseOut( $listener:Function ):void{
			addEvent( '_out', $listener );
		}
		private function addEvent( $event:String, $listener:Function ):void{
			var temp:*;
			temp = _parent[$event][this];
			if( temp ){}else{
				_parent[$event][this] = new Vector.<Function>();
				temp = _parent[$event][this]; 
			}
			if( temp.indexOf( $listener ) < 0 ){
				temp[temp.length] = $listener;
			}
		}
		internal function removeOnMouseDown( $listener:Function ):void{
			removeEvent( '_down', $listener );
		}
		internal function removeOnMouseUp( $listener:Function ):void{
			removeEvent( '_up', $listener );
		}
		internal function removeOnMouseOver( $listener:Function ):void{
			removeEvent( '_over', $listener );
		}
		internal function removeOnMouseOut( $listener:Function ):void{
			removeEvent( '_out', $listener );
		}
		private function removeEvent( $event:String, $listener:Function ):void{
			var temp:*;
			temp = _parent[$event][this];
			if( temp ){
				temp.splice( temp.indexOf( $listener ), 1 );
			}
		}
	}
}